package com.calefk.core {
    
	import flash.display.MovieClip;

    public class Animation extends EntitySimple {
        
        public var isLoop:Boolean;
        public var isEnd :Boolean;
        private var mTotalFrames:int;
        private var mCurrentFrame:int;
        private var mFunctions:Array;
        
        public function get currentFrame():int { return mCurrentFrame; }
        public function get totalFrames() :int { return mTotalFrames;  }
        
        public function Animation(canvas:MovieClip, nameClip:String = null, px:Number = 0, py:Number = 0 ,
                _isLoop:Boolean = false) {
                    
            super(canvas, nameClip, px, py);
			mCurrentFrame = 1;
            mTotalFrames = mClip.totalFrames;
            isLoop = _isLoop;
            mFunctions = new Array();
        }
        
        public function start():void {
            mCurrentFrame = 1;
            mClip.gotoAndStop(mCurrentFrame);
            isEnd = false;
        }
        
        public function addExecute(frame:int , func_exe:Function):void {
            mFunctions["f" + frame] = func_exe;
        }
        
        override public function update(dt:int):void {
            if (isEnd) { return; }
            // update and execute function
            mClip.gotoAndStop(mCurrentFrame);
            
            // execute function
            if (mFunctions["f" + mCurrentFrame]) {
                mFunctions["f" + mCurrentFrame]();
            }
            
            mCurrentFrame++;
            
            // verificar si es loop
            if (mCurrentFrame > mTotalFrames && isLoop) {
                mCurrentFrame = 1;
            }
            
            // verificar si termino y no es loop
            if (mCurrentFrame > mTotalFrames && !isLoop) {
                isEnd = true;
                mCurrentFrame = mTotalFrames;
            }
        }
        
        public function visible(value:Boolean):void {
            mClip.visible = value;
        }
        
        override public function clean():void {
            for (var key:String in mFunctions) {
                mFunctions[key] = null;
            }
            mFunctions = null;
            super.clean();
        }
    }
}